//
//  AppDelegate.m
//  01-UIView、UIWindow
//
//  Created by QC.L on 16/7/14.
//  Copyright © 2016年 QC.L. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import "UIView+Frame.h"
@interface AppDelegate ()<UITextFieldDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 程序的组成结构
    // 1.main.m 主要实现了程序的正常运行
    // 2. AppDelegate 程序的执行者，签订了UIAplicationDelegate (协议方法可选)
// 注意：AppDelegate就是一个普通的类 只不过 签订了协议，才有了这么多方法
    
    //3. ViewController 视图控制器  主要负责视图管理 看得见 摸得到的
    // 4.Main.sb（视图管理） LauchScreen.sb （负责启动页）可视化管理 （Xcode 7以前 LaunchScreen.xib）
    // 5.Assets.xcassets 主要用来管理图片素材
      // (Images.xcassets)  Xcode 7 以前叫这个
    // 6.Info.plist (工程配置文件)
    
    // 模拟器
    // command + 1~5 改变大小
    // command + ⬅️  → 横竖屏切换
    
    // iOS 概述
    // 1. UIWindown (程序中，只有一个主Windown)
    // 1> 取消程序的可视化入口，Main.sb
    // 2> 创建主windown 对象
    
    

    
    // 创建主windown 使其大小与屏幕一样
    // UIScreen 系统屏幕类
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    // 给创建的窗口填颜色 UIColor 系统颜色类
    self.window.backgroundColor = [UIColor whiteColor];
    ViewController *vc = [[ViewController alloc]init];
    self.window.rootViewController = vc;
    //让windown 成为主窗口 并显示
    [self.window makeKeyAndVisible];
    
#if 0
    // 3>UIWindown 继承于 UIView
    
    //2. UIView (与NSObject相似，是UI阶段的根类， 大部分UI控件继承于UIView)
    
    
   // 学习新类
    // 1> 需要新类包含哪些初始化方法
    // 2> 包含哪些行为（方法）和特征 （属性）
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 250, 250)];
    view.backgroundColor = [UIColor redColor];
    [self.window addSubview:view];
    
    //view.frame = CGRectMake(10, 10, 80, 80);
    NSLog(@"改变bouds前%@",NSStringFromCGRect(view.frame));
    NSLog(@"%@",NSStringFromCGPoint(view.center));
   view.bounds = CGRectMake(0, 0, 100, 100);
    NSLog(@"改变bouds后%@",NSStringFromCGRect(view.frame));
    NSLog(@"%@",NSStringFromCGPoint(view.center));

    UIView *subView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    subView.backgroundColor = [UIColor greenColor];
    [view addSubview:subView];
    
    
    // iOS 系统坐标系
    // 1.与数学中的坐标系不同， y的正方向是向下的
    // 2.原点是屏幕的左下角
    
    
    // frame and center
    // 1.frame (x,y,width,height) 视图在其父视图的上的位置
    // 2. bounds (x,y,width,height) 视图本身的坐标系
    // bounds x,y修改的本身的坐标系，影响他的子视图
    // bounds width height 修改的是本身的大小  会以中心点收缩/ 扩张
    //  bounds 的改变 不会改变视图的位置
    
    
    //3. center 中心点
    
    // bounds 对视图的影响测试
    // bounds 的 x y 影响子视图 的frame ,改变坐标系
    
    // 改变红色的view 大小  width变成100
    
    // view.frame get 方法
    // frame.size 结构体
    // OC和C 的内容结合， 是一个常量
    // view.frame.size.width = 100;
    view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, 100, view.frame.size.height);
    
    
    view.yj_width = 100;
#endif
#if 0
    UIView *testView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    testView.backgroundColor = [UIColor cyanColor];
    //添加
    [self.window addSubview:testView];
    //移除
  //  [testView removeFromSuperview];
    
    testView.alpha = 0.2;
    testView.tag = 1000;
    // 根据tag 获取到对应的view __kind of
  UIView *view = [self.window viewWithTag:1000];
    //4.subviews 子视图
    UIView *arrayView = [_window.subviews objectAtIndex:0];
    NSLog(@"%@",arrayView);
    
    //5.superview 父视图
   // testView.superview
    
    //hidden
    testView.hidden = NO;
    
    UIView *firstView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 150, 150)];
    [_window addSubview:firstView];
    firstView.backgroundColor = [UIColor redColor];
    UIView *secondView = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 250, 250)];
    
    secondView.backgroundColor = [UIColor greenColor];
    UIView *thirdView = [[UIView alloc]initWithFrame:CGRectMake(20, 20, 200, 200)];
    thirdView.backgroundColor = [UIColor blueColor];
    // 视图的层级
   // [testView insertSubview:<#(nonnull UIView *)#> atIndex:<#(NSInteger)#>]  插入视图 到指定的下标
    [_window insertSubview:secondView atIndex:2];

    
  // [testView insertSubview:<#(nonnull UIView *)#> aboveSubview:<#(nonnull UIView *)#>]插入视图  到指定的视图上
    
    [_window insertSubview:thirdView aboveSubview:firstView];
    
 //   [testView insertSubview:<#(nonnull UIView *)#> belowSubview:<#(nonnull UIView *)#>]插入视图  到指定的视图下
    
#endif
   // UILable  (负责显示文本)
    // 如果类没有自己的初始化方法 则使用父类
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 180, 80)];
    [self.window addSubview:label];
    label.backgroundColor = [UIColor yellowColor];
    label.text = @"用户名jkjkjkdjfkjdskfjdjfjdkjfjdjfjdsfjdsfjkjkdjfkdfksdjk";
    // 对齐方式
    label.textAlignment = 2;
    label.textColor = [UIColor redColor];
    //font 默认值17
    label.font = [UIFont systemFontOfSize:18];
    label.font = [UIFont boldSystemFontOfSize:20];
    // 行数 默认是1行   不确定是 给0 行
    label.numberOfLines = 3;
    //断行模式
    label.lineBreakMode = NSLineBreakByTruncatingMiddle;
    
    //
    label.shadowOffset = CGSizeMake(3, 5);
    label.shadowColor = [UIColor blueColor];
    
    
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(200, 200, 100, 40);
    [self.window addSubview:button];
    [button setTitle:@"确定" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor blueColor];
    [button setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(handleOK:) forControlEvents:UIControlEventTouchUpInside];
    
    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(10, 10, 200, 40)];
    [self.window addSubview:textField];
    textField.backgroundColor = [UIColor lightGrayColor];
    // 当text不为空时，placeholder不显示
    textField.placeholder = @"输入密码";
    textField.textColor = [UIColor redColor];
    textField.secureTextEntry = YES;
    textField.keyboardType = UIKeyboardAppearanceDefault;
//    UITextBorderStyleNone,无
//    UITextBorderStyleLine, 有边缘线
//    UITextBorderStyleBezel,
//    UITextBorderStyleRoundedRect 边缘呈圆角
    
    textField.borderStyle = UITextBorderStyleBezel;
    textField.clearButtonMode = UITextFieldViewModeAlways;
    // clearOnBeginEdting
    textField.clearsOnBeginEditing = YES;
    textField.delegate = self;
    
    
    
    // Override point for customization after application launch.
    return YES;
}
- (void)handleOK:(UIButton *)button {
    //结束编辑
    [self.window endEditing:YES];
    NSLog(@"dd");
}
// 判断是否开始编辑
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    return YES;
}
// 已经开始编辑
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    
}
//判断是否结束编辑
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    
}
// 点return  回收键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    // 处理点击return，需要进行的操作
    // 回收键盘
    [textField endEditing:YES];
    //[textField resignFirstResponder];
    return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
